package com.example.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Goods;
import com.example.domain.Order;
import com.example.mapper.GoodsMapper;
import com.example.mapper.OrderMapper;
import com.example.service.IGoodsService;
import com.example.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.Result;
import org.apache.logging.log4j.util.Strings;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.*;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private String key = "order";

    @Transient
    @Override
    public Result addOrder(Order order, Goods goods) {


        Boolean order1 = false;
        try {
//            goodsMapper.updateById(goods);
            order1 = orderMapper.addOrder(order);
            if (!order1) {
                return new Result(false, "添加采购单错误");
            }
            return goodsService.updateStock(goods);
//            return new Result(true, "添加采购单成功");
        } catch (Exception e) {
            return new Result("非法操作，请联系管理员");
        }
    }

    @Override
    public Result updateOrder(Order order) {

        if (!orderMapper.updateOrder(order)) {
            return new Result("修改采购单信息错误");
        }
        stringRedisTemplate.opsForHash().delete(key, order.getCid().toString());

        return new Result("修改采购单信息成功");
    }

    @Override
    public Result findById(Integer id) {

        Object orderStr = stringRedisTemplate.opsForHash().get(key, id.toString());

        if (orderStr != null && orderStr != "") {

            return new Result(true, JSONUtil.toBean((String) orderStr, Order.class));
        }

        Order byId = orderMapper.findById(id);
        if (byId == null) {
            return new Result("查询失败");
        }

        stringRedisTemplate.opsForHash().put(key, id.toString(), JSONUtil.toJsonStr(byId));
        return new Result(true, byId);
    }

    @Override
    public Result selectAll() {

        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
        List<Order> orders;
        if (entries != null && entries.size() != 0) {
            orders = new ArrayList<>();
            entries.forEach((key, value) -> {
                orders.add(JSONUtil.toBean((String) value, Order.class));
            });
            return new Result(true, orders);
        }

        orders = orderMapper.selectAll();
        if (orders == null) {
            return new Result("查询失败");
        }

        Map<String, String> orderMap = new HashMap<>();

        for (Order order : orders) {
//            stringRedisTemplate.opsForHash().put(key,order.getCid().toString(),JSONUtil.toJsonStr(order));

            orderMap.put(order.getCid().toString(), JSONUtil.toJsonStr(order));
        }

        stringRedisTemplate.opsForHash().putAll(key, orderMap);

        return new Result(true, orders);
    }



    @Override
    public Result selectByBusinessOrder(String business) {
        List<Order> orders = orderMapper.selectByBusinessOrder(business);
        if (orders != null && orders.size() != 0) {
            return new Result(true, orders);
        }
        return new Result("查询失败");
    }


}
